<script lang="ts" setup name="XtxCarousel">
import { IBannerItem } from '@/types/goods'
import { onMounted, onUnmounted, ref } from 'vue'
// #1 接收 autoPlay 和 duration
const {
  autoPlay = false,
  duration = 3000,
  slides,
} = defineProps<{
  slides: IBannerItem[]
  autoPlay?: boolean
  duration?: number
}>()
// 需求：点击切换图片和小圆点
const active = ref(0)

const onPrev = () => {
  active.value--
  if (active.value < 0) {
    active.value = slides.length - 1
  }
}

const onNext = () => {
  active.value++
  if (active.value > slides.length - 1) {
    active.value = 0
  }
}

const play = () => {
  // #2 根据 autoPlay 控制
  if (!autoPlay) return
  clearInterval(timer)
  // 模块化环境中，直接写的 setInterval 会默认使用的是 Node 的定时器
  // 其实我想用的是浏览器的，加 window 就行了
  timer = window.setInterval(() => {
    onNext()
    // #3 写死的时间换成 duration
  }, duration)
}

// timer 其实没有视图中用到，没有必要用 ref 包裹成响应式的
let timer = -1

onMounted(() => {
  // 只能控制一上来的情况
  // 划上去再离开的时候还是可以自动播放的
  // if (!autoPlay) return
  play()
})
onUnmounted(() => clearInterval(timer))
const stop = () => clearInterval(timer)
</script>

<template>
  <div class="xtx-carousel" @mouseenter="stop" @mouseleave="play">
    <!-- {{ props.slides[0].hrefUrl }} -->
    <!-- slides 能直接用吗？ -->
    <ul class="carousel-body">
      <!-- 循环轮播图 -->
      <li class="carousel-item" v-for="(item, index) in slides" :key="item.id" :class="{ fade: active === index }">
        <RouterLink :to="`/goods/${item.id}`">
          <img :src="item.imgUrl" alt="" />
        </RouterLink>
      </li>
    </ul>
    <a @click="onPrev" href="javascript:;" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a>
    <a @click="onNext" href="javascript:;" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a>
    <div class="carousel-indicator">
      <!-- 循环小点点 -->
      <span :class="{ active: active === index }" v-for="(item, index) in slides" :key="item.id" @mouseenter="active = index"></span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
